package com.carlosk.kandicar.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.carlosk.kandicar.ui.base.BaseApp;

/**
 * 文件操作工具类
 * @author liujiaqi
 * @creationTime 2014-3-5下午1:28:09
 * @version 
 *
 */
public class FileUtils {
	

	/**
	 * 创建文件
	 * @author liujiaqi
	 * @creation time 2014-3-5下午1:17:52
	 * @version
	 * @param path
	 * @param name
	 */
	public static File createFile(String path, String name) throws IOException {
		String filePath = path + File.separator + name;
		File file = new File(filePath);
		if (!file.exists()) {
			File dir = createFolder(path);
			if(dir.exists()){
				file.createNewFile();
			}else{
				throw new IOException("文件夹创建失败："+dir.getPath());
			}
		}
		return file;
	}
	
	
	/**
	 * 创建文件夹
	 * @author liujiaqi
	 * @creation time 2014-3-5下午1:21:49
	 * @version 
	 * @param path
	 * @throws IOException
	 */
	public static File createFolder(String path) throws IOException {
		String filePath = path;
		File file = new File(filePath);
		if (!file.exists()) {
			file.mkdirs();
		}
		return file;
	}
	
	
	/**
	 * 删除文件
	 * @author liujiaqi
	 * @creation time 2014-3-5下午1:27:45
	 * @version 
	 * @param path
	 * @param name
	 * @throws IOException
	 */
	public static void delFile(String path, String name) throws IOException {
		String filePath = path + "/" + name;
		File file = new File(filePath);
		if(file.exists()){
			file.delete();
		}
	}
	
	/**
	 * 删除文件夹
	 * @author liujiaqi
	 * @creation time 2014-3-5下午1:27:56
	 * @version 
	 * @param path
	 * @throws IOException
	 */
	public static void delFolder(String path) throws IOException {
		String filePath = path;
		File file = new File(filePath);
		if (file.exists()) {
			file.delete();
		}
	}
	
	
	/**
	 * 获取文件全称，包含后缀名
	 * @author liujiaqi
	 * @creation time 2014-3-5下午3:37:23
	 * @version 
	 * @param path
	 * @return
	 * @throws Exception
	 */
	public static String getFileNameByPath(String path) throws Exception{
		if(path.indexOf(".") !=-1){
			int startIndex = path.lastIndexOf("/");
			return path.substring(startIndex+1);
		}
		return "";
	}
	
	
	/**
	 * 获取文件夹下面的所有文件
	 * @author liujiaqi
	 * @creation time 2014-3-6上午10:00:38
	 * @version 
	 * @param path
	 * @return
	 * @throws Exception
	 */
	public static File[] getFilesByFolderPath(String path) throws Exception {
		File folder = new File(path);
		return folder.listFiles();
	}
	
	
	/**
	 * 
	 * @author liujiaqi
	 * @creation time 2014-3-13下午5:53:31
	 * @version 
	 * @param filePath
	 * @return
	 */
	public static boolean isFileExists(String filePath){
		File file = new File(filePath);
		return file.exists();
	}
	
	
	/**
	 * 将一个文件写到对应的目录里面去
	 * @param path
	 * @param fileName
	 * @param input
	 * @return
	 */
	public static File writeFileToSDFromInput(String path,String fileName,InputStream input) throws Exception{
		File file = null;
		OutputStream output = null;
		try{
			createFolder(path);
			file = createFile(path, fileName);
			output = new FileOutputStream(file);
			byte[] buffer = new byte[4*1024];
			while((input.read(buffer)) != -1){
				output.write(buffer);
			}
			output.flush();
		}finally{
			try{
				if(output !=null){
					output.close();
					output = null;
				}
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
		return file;
	}
	
	/**
	 * 将文件转换为二进制字符串
	 * @author liujiaqi
	 * @creation time 2014-4-17下午5:03:45
	 * @version 
	 * @param file
	 * @throws IOException
	 */
	public static String FileToByteString(File file) throws IOException {  
        FileInputStream fileInputStream = new FileInputStream(file);  
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();  
        int i; 
        //转化为字节数组流  
        while ((i = fileInputStream.read()) != -1) {  
            byteArrayOutputStream.write(i);  
        }  
        fileInputStream.close();  
        // 把文件存在一个字节数组中  
        byte[] filea = byteArrayOutputStream.toByteArray();  
        byteArrayOutputStream.close();  
        
        String encoding = "ISO-8859-1";  
        String byteString = new String(filea, encoding);  
        return byteString;
    }
	
	/**
	 * 将二进制字符串转换为byte数组
	 * @author liujiaqi
	 * @creation time 2014-4-17下午5:08:27
	 * @version 
	 * @param byteString
	 * @return
	 * @throws IOException
	 */
	public static byte[] ByteStringToByteArray(String byteString) throws IOException {
        String encoding = "ISO-8859-1";
        return byteString.getBytes(encoding);
	}
	
	/**
	 * 将二进制流写入到文件里面
	 * @author liujiaqi
	 * @creation time 2014-4-17下午5:15:19
	 * @version 
	 * @param file
	 * @param byteArray
	 * @throws IOException
	 */
	public static void writeFileByByteArray(File file,byte[] byteArray) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(file);  
        fileOutputStream.write(byteArray);  
        fileOutputStream.flush();  
        fileOutputStream.close();  
	}
	
	/**
	 * 获取应用的缓存路径
	 * @author liujiaqi
	 * @creation time 2014-3-6上午9:55:55
	 * @version 
	 * @return
	 */
	public static String getBasePath() {
		return BaseApp.AppContext.getCacheDir().getPath();
	}

}
